<?php
/**
 * Created by PhpStorm.
 * User: wxy
 * Date: 2018/11/8
 * Time: 16:22
 */

namespace frontend\modules\common\models;


use PHPExcel_CachedObjectStorageFactory;
use PHPExcel_IOFactory;
use PHPExcel_Settings;

class ExcelReader
{
    /**
     * @param $path
     * @return array
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public function NormalReadExcel($path, $excelHeader)
    {

        header("Content-Type:text/html;charset=utf-8");

        $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
        PHPExcel_Settings::setCacheStorageMethod($cacheMethod);

        $objPHPExcel = PHPExcel_IOFactory::load($path);

        $sheet = $objPHPExcel->getSheet(0);
        echo "初始: " . memory_get_usage() . "B<br>";
        //获取数据
        $data = array();
        foreach ($sheet->getRowIterator() as $key => $row) {

            $cellIterator = $row->getCellIterator();   //得到所有列
            $cellIterator->setIterateOnlyExistingCells(false); // Loopall cells, even if it is not set
            foreach ($cellIterator as $k => $cell) {  //遍历列
                $data[$key][$excelHeader[$k]] = trim($cell->getCalculatedValue());
            }
            if ($key == 10001) {
                break;
            }
            unset($cellIterator);
        }
        echo "读完: " . memory_get_usage() . "B<br>";
        $objPHPExcel->disconnectWorksheets();
        return $data;

    }

}